package com.shuorikeji.huohuo.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.UserInfoMainBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.common.Constant;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.ui.home.HomeActivity;
import com.shuorikeji.huohuo.utils.BusinessUtils;
import com.shuorikeji.huohuo.utils.CountDownTimer;
import com.shuorikeji.huohuo.utils.JsonUtil;
import com.shuorikeji.huohuo.utils.PreUtils;
import com.shuorikeji.huohuo.utils.ToastShowUtil;

import butterknife.BindView;

public class LoginActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_yzm)
    EditText etYzm;
    @BindView(R.id.tv_get_yzm)
    TextView tvGetYzm;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.iv_sec)
    ImageView ivSec;
    @BindView(R.id.tv_user_xy)
    TextView tvUserXy;
    @BindView(R.id.tv_ys_xy)
    TextView tvYsXy;
    @BindView(R.id.et_tgm)
    EditText etTgm;

    private boolean isAgree;
    private CountDownTimer djsTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_login);
    }

    @Override
    protected void initData() {
        isAgree = false;
        initClick();
    }

    private void login() {
        mPresenter.login(etPhone.getText().toString(), etYzm.getText().toString(), etTgm.getText().toString(), "login", true, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        UserInfoMainBean data = (UserInfoMainBean) object;
                        PreUtils.put(Constant.LOGIN_USER_INFO, JsonUtil.toJson(data.getData()));
                        Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                        startActivity(intent);
                        finish();
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(LoginActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void startDjs() {
        djsTimer = new CountDownTimer(60, 1000);
        djsTimer.start(new CountDownTimer.OnTimerCallBack() {
            @Override
            public void onStart() {
                Log.i("wxl", "验证码倒计时开始");
            }

            @Override
            public void onTick(long times) {
                tvGetYzm.setText(times + "s");
            }

            @Override
            public void onFinish() {
                Log.i("wxl", "验证码倒计时结束");
                tvGetYzm.setText("获取验证码");
            }
        });
    }

    private void getYzm() {
        mPresenter.sendYzm(true, etPhone.getText().toString(), "login", new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        startDjs();
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(LoginActivity.this, msg);
                        break;
                }
            }
        });
    }

    /**
     * 清理计时器
     */
    private void clearYzmDjs() {
        if (djsTimer != null) {
            djsTimer.cancel();
        }
    }

    private void initClick() {
        tvGetYzm.setOnClickListener(view -> {
            if ("获取验证码".equals(tvGetYzm.getText().toString())) {
                if (BusinessUtils.isEmpty(etPhone.getText().toString())) {
                    ToastShowUtil.showToastCenter(LoginActivity.this, "请输入手机号");
                } else {
                    getYzm();
                }
            } else {
                ToastShowUtil.showToastCenter(LoginActivity.this, "验证码已发送，请稍后再试");
            }
        });
        tvLogin.setOnClickListener(view -> {
            if (isAgree) {
                if (BusinessUtils.isEmpty(etYzm.getText().toString())) {
                    ToastShowUtil.showToastCenter(LoginActivity.this, "请输入验证码");
                } else {
                    clearYzmDjs();
                    login();
                }
            } else {
                ToastShowUtil.showToastCenter(LoginActivity.this, "请阅读并同意《用户协议》及《隐私协议》");
            }
        });
        ivSec.setOnClickListener(view -> {
            if (isAgree) {
                isAgree = false;
                ivSec.setImageResource(R.mipmap.icon_un_sec_yellow);
            } else {
                isAgree = true;
                ivSec.setImageResource(R.mipmap.icon_sec_yellow);
            }
        });
        tvUserXy.setOnClickListener(view -> {
            Intent intent = new Intent(LoginActivity.this, WebViewActivity.class);
            intent.putExtra("title", "用户协议");
            startActivity(intent);
        });
        tvYsXy.setOnClickListener(view -> {
            Intent intent = new Intent(LoginActivity.this, WebViewActivity.class);
            intent.putExtra("title", "隐私协议");
            startActivity(intent);
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        clearYzmDjs();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        clearYzmDjs();
    }
}